<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/style.css">
    <title>电子医生</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            padding: 20px;
            background-color: #f8f9fa;
        }
        .chat-container {
            max-width: 600px;
            margin: 0 auto;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }
        .form-floating {
            margin-bottom: 20px;
        }
        #conversationHistory ul {
            list-style-type: none;
            padding: 0;
        }
        #conversationHistory li {
            margin-bottom: 10px;
            padding: 10px;
            border-radius: 4px;
            background-color: #e9ecef;
        }
        #conversationHistory li.user {
            background-color: #d1ecf1;
        }
        #conversationHistory li.ai {
            background-color: #f8d7da;
        }
        .form-floating label {
            transition: all 0.2s ease-in-out;
        }

        /* 当textarea获得焦点时，label消失 */
        .form-floating .form-control:focus ~ label,
        .form-floating .form-control:not(:placeholder-shown) ~ label {
            display: none;
        }

        /* 使得label的初始位置正确 */
        .form-floating .form-control {
            padding-top: 1.5rem; /* 预留空间给label */
        }
    </style>
</head>
<body>
<!-- 加载header.html -->
<div th:insert="~{user/header}"></div>
<main id="main">


    <!--副标题-->
    <section class="breadcrumbs" style="margin-top: 85px;">
        <div class="container">
            <div class="d-flex justify-content-between align-items-center">
                <h2 id="pageTitle">电子医生</h2>
                <ol>
                    <li><a href="index.html">首页</a></li>
                    <li id="breadcrumbItem"><a href="#" ></a>电子医生</li>
                </ol>
            </div>
        </div>
    </section>
</main>
</main>
    <div class=" inner-page text-center" style="width: 1200px; background-color: #e5fcfb; margin-top: 10px; display: flex; align-items: center; justify-content: space-between; margin-left: auto; margin-right: auto;">
        <div class="card-image">
            <img src="img/robot.jpg" alt="robot" style="width: 550px; height: 600px; object-fit: cover;">
        </div>
        <div class="chat-container card-body" style="width: 600px; height: 550px; background-color: white; /* 添加背景颜色以便于查看 */">
            <h1 class="mb-4" style="font-family: 'Microsoft YaHei';">问题咨询</h1>
            <div class="form-floating" style="display: flex;  margin-left: 20px">
                <textarea class="form-control" placeholder="很高兴为您服务，您可以随时向我咨询病情" id="floatingTextarea" style="height: 100px; width: 400px"></textarea>
                <label for="floatingTextarea">请输入您的问题</label>
                <button type="button" class="btn btn-info mt-3" id="sendButton" style="margin-top: 10px;background-color: #0accce;height: 40px;margin-left: 15px">发送</button>
            </div>


            <div id="conversationHistory" class="mt-4" style="max-height: 300px; overflow-y: auto;">
                <ul>
                    <li th:each="entry : ${conversationHistory}" th:text="${entry.key}" th:class="${entry.key.startsWith('用户')} ? 'user' : 'ai'"></li>
                </ul>
            </div>
        </div>
    </div>
<script>
    $(document).ready(function() {
        $('#sendButton').click(function() {
            var message = $('#floatingTextarea').val();
            $.ajax({
                url: '/ai/chat',
                type: 'GET',
                data: { message: message },
                success: function(response) {
                    updateConversationHistory("用户", message);
                    updateConversationHistory("电子医生", response);
                    // 清空输入框的内容
                    $('#floatingTextarea').val('');
                },
                error: function(xhr, status, error) {
                    console.error('Error:', error);
                }
            });
        });

        function updateConversationHistory(speaker, entry) {
            var liClass = speaker === "用户" ? "user" : "ai";
            // 将新消息添加到聊天记录的顶部
            $('#conversationHistory ul').prepend('<li class="' + liClass + '">' + speaker + ': ' + entry + '</li>');
            if ($('#conversationHistory li').length > 10) {
                // 移除最早的两条消息
                $('#conversationHistory li:gt(7)').remove();
            }
        }
    });
</script>
</body>
</html>

